<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title></title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../layui/css/layui.css">
    <script src="../js/jquery.min.js"></script>
</head>
<body>

<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>产品管理</legend>
</fieldset>

<table class="layui-hide" id="test" lay-filter="test"></table>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
        <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
        <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
    </div>

</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>


<script src="../layui/layui.js" charset="utf-8"></script>


<script>
    layui.use('table', function(){
        var table = layui.table;

        table.render({
            elem: '#test'
            ,url:'/admin/chaXunAll'
            ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            ,defaultToolbar: ['filter', 'exports', 'print']
            ,title: '用户数据表'
            ,cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field:'ano', title:'账号', width:150, fixed: 'left', unresize: true, sort: true}
                ,{field:'aname', title:'姓名', width:250, edit: 'text'}
                ,{field:'arole', title:'角色', width:250, edit: 'text'}
                ,{fixed:'right', title:'操作', toolbar: '#barDemo'}
            ]]
            ,page: true
        });

        //头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：'+ data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选': '未全选');
                    break;

                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
                case 'add':
                    layer.open({
                        type: 2,
                        title: '添加界面',
                        area: ['350px', '350px'],
                        content:'addAdmin.html'

                    });

            };
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            //console.log(obj)
            if(obj.event === 'del'){
                layer.confirm("真的删除行吗？",{
                    btn: ['删除', '取消']
                }, function () {
                    obj.del();
                    $.ajax({
                        url: "/admin/shanChuById",
                        method:"post",
                        data:{
                            id:data.ano
                        },
                        success:function (res) {
                            if(res.code==200){
                                layer.msg("删除成功");

                            }else {
                                layui.use('layer', function(){
                                    var layer = layui.layer;

                                    layer.msg('删除失败');
                                });
                            }
                            layer.close();
                        }
                    });
                }, function(){
                    layer.close();
                });
            } else if(obj.event === 'edit'){
                layer.open({
                    type: 1,
                    title:'编辑界面',
                    content:`
                     <form class="layui-form" action="/admin/gengXinById" method="post">
                       <div class="layui-form-item">
                             <label class="layui-form-label">账号：</label>
                          <div class="layui-input-block">
                               <input type="text" name="ano" required  lay-verify="required" value="${data.ano}" autocomplete="off" class="layui-input">
                             </div>
                          </div>
                       <div class="layui-form-item">
                             <label class="layui-form-label">姓名：</label>
                          <div class="layui-input-block">
                               <input type="text" name="aname" required  lay-verify="required" value="${data.aname}" autocomplete="off" class="layui-input">
                             </div>
                          </div>
                          <div class="layui-form-item">
                             <label class="layui-form-label">角色：</label>
                          <div class="layui-input-block">
                               <input type="text" name="arole" required  lay-verify="required" value="${data.arole}" autocomplete="off" class="layui-input">
                             </div>
                          </div>

                            <div class="layui-form-item">
                            <div class="layui-input-block">
                                  <button TYPE="submit" lay-submit lay-filter="formDemo" CLASS="layui-btn">立即提交</button>
                                   <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                            </div>
                            </div>
                         </form>
`


                });
            }
        });
    });



</script>

</body>
</html>